<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{

    //进行遍历类别
    static public function getCate($pid){
        //定义一个空数组
        $cate=array();
        //通过传过来的pid查询类别表
        $data=DB::table('cate')->where('pid','=',$pid)->get();
        //遍历查询到的数据
        foreach($data as $k=>$v){
            //通过自身的id去掉用自身的方法(通过pid查询) 判断下面是否还存在子分类 如果有就把他压入自身的一个下标中
            $v['fenlei']=self::getCate($v['cid']);
            //将每次得到的数据遍历到空数组中
            $cate[]=$v;
        }
         return $cate;
    } 
    public function getIndex(Request $request){
        $keyword=$request->input('keyword');
        $cid=$request->input('cid');
        //先查类别表中所有路径里面含有传过来的类别id 便于遍历出一种类别下的所有商品
        $cate=DB::table('cate')->select('cid')->where('path','like',"%".','.$cid.','."%")->get();
        
        //遍历查询到的数据 得到所有cid
        $num=array();
        foreach($cate as $k=>$v){
            $num[]=$v['cid'];
        }
        //把本身的ID压入数组当中
        $num[]=$cid;

        //查询出商品表中cid等于上面查到的所有cid的数据
        $data=DB::table('goods')->whereIn('cid',$num)->where('gstatus','<','2')->orderBy('gprice','asc')->paginate(2);
        // dd($data);
        $info=DB::table('goods')->where('gname','like','%'.$keyword.'%')->orWhere('gdesc','like','%'.$keyword.'%')->orderBy('gprice','asc')->paginate(2);
        // dd($info);
        //查询到商品的件数
        $sum=DB::table('goods')->whereIn('cid',$num)->where('gstatus','<','2')->get();
        // dd($data);
        $sum=count($sum);
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();
        
        //把得到的数据发送到模板进行遍历
        $cate=self::getCate(0);
        return view('/home/goods/index',['cate'=>$cate,'hot'=>$hot,'info'=>$info,'sum'=>$sum,'cid'=>$cid,'data'=>$data]);
   }

   //按销量进行排序
   public function getPorderby(Request $request){
         $cid=$request->input('cid');
    //先查类别表中所有路径里面含有传过来的类别id 便于遍历出一种类别下的所有商品
    $cate=DB::table('cate')->select('cid')->where('path','like',"%".','.$cid.','."%")->get();
    
    //遍历查询到的数据 得到所有cid
    $num=array();
    foreach($cate as $k=>$v){
        $num[]=$v['cid'];
    }
    //把本身的ID压入数组当中
    $num[]=$cid;

    //查询出商品表中cid等于上面查到的所有cid的数据
    $data=DB::table('goods')->whereIn('cid',$num)->where('gstatus','<','2')->orderBy('gsold','desc')->paginate(2);
    //查询到商品的件数
    $sum=DB::table('goods')->whereIn('cid',$num)->where('gstatus','<','2')->get();
    // dd($data);
    $sum=count($sum);
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();

    //把得到的数据发送到模板进行遍历
    $cate=self::getCate(0);
    return view('/home/goods/index',['cate'=>$cate,'hot'=>$hot,'sum'=>$sum,'cid'=>$cid,'data'=>$data]);
   }

   //按创建时间进行排序
   public function getCorderby(Request $request){
         $cid=$request->input('cid');
    //先查类别表中所有路径里面含有传过来的类别id 便于遍历出一种类别下的所有商品
    $cate=DB::table('cate')->select('cid')->where('path','like',"%".','.$cid.','."%")->get();
    
    //遍历查询到的数据 得到所有cid
    $num=array();
    foreach($cate as $k=>$v){
        $num[]=$v['cid'];
    }
    //把本身的ID压入数组当中
    $num[]=$cid;
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();

    //查询出商品表中cid等于上面查到的所有cid的数据
    $data=DB::table('goods')->whereIn('cid',$num)->where('gstatus','<','2')->orderBy('createTime','asc')->paginate(2);
   // dd($page);
    //查询到商品的件数
    $sum=DB::table('goods')->whereIn('cid',$num)->where('gstatus','<','2')->get();
    // dd($data);
    $sum=count($sum);

    //把得到的数据发送到模板进行遍历
    $cate=self::getCate(0);
    return view('/home/goods/index',['cate'=>$cate,'hot'=>$hot,'sum'=>$sum,'cid'=>$cid,'data'=>$data]);
   }


   //通过关键字查询
   public function getZchaxun(Request $request){
        $keyword=$request->input('keyword');
        if(!empty($keyword)){
            $info=DB::table('goods')->where('gstatus','<','2')->where('gname','like','%'.$keyword.'%')->orWhere('gdesc','like','%'.$keyword.'%')->paginate(2);
        }else{
            $info=DB::table('goods')->where('gstatus','<','2')->paginate(2);
        }
        // dd($info);
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();
        
        $sum=count($info);
        $cate=self::getCate(0);
        // dd($info);
        return view('/home/goods/index',['sum'=>$sum,'hot'=>$hot,'data'=>$info,'keyword'=>$keyword,'cate'=>$cate]);
        
   }
   //默认设置
   public function getKindex(Request $request){
         $keyword=$request->input('keyword');
         if(!empty($keyword)){
            $info=DB::table('goods')->where('gstatus','<','2')->where('gname','like','%'.$keyword.'%')->orWhere('gdesc','like','%'.$keyword.'%')->orderBy('gprice','asc')->paginate(2);
         }else{
              $info=DB::table('goods')->where('gstatus','<','2')->paginate(2);
        }
         $sum=count($info);
         $cate=self::getCate(0);
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();
         
         return view('/home/goods/index',['sum'=>$sum,'hot'=>$hot,'data'=>$info,'keyword'=>$keyword,'cate'=>$cate]);
   }

   //按照销量拍序
   public function getKporderby(Request $request){
         $keyword=$request->input('keyword');
         if(!empty($keyword)){
            $info=DB::table('goods')->where('gstatus','<','2')->where('gname','like','%'.$keyword.'%')->orWhere('gdesc','like','%'.$keyword.'%')->orderBy('gsold','desc')->paginate(2);
         }else{
              $info=DB::table('goods')->where('gstatus','<','2')->paginate(2);
        }
         $sum=count($info);
         $cate=self::getCate(0);
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();
         
         return view('/home/goods/index',['sum'=>$sum,'hot'=>$hot,'data'=>$info,'keyword'=>$keyword,'cate'=>$cate]);
   }

   //按照创建时间拍序
   public function getKcorderby(Request $request){
         $keyword=$request->input('keyword');
         if(!empty($keyword)){
            $info=DB::table('goods')->where('gstatus','<','2')->where('gname','like','%'.$keyword.'%')->orWhere('gdesc','like','%'.$keyword.'%')->orderBy('createTime','desc')->paginate(2);
         }else{
            $info=DB::table('goods')->where('gstatus','<','2')->paginate(2);
        }
         $sum=count($info);
         $cate=self::getCate(0);
        $hot=DB::table('goods')->orderBy('hot','')->limit(5)->get();

         return view('/home/goods/index',['sum'=>$sum,'hot'=>$hot,'data'=>$info,'keyword'=>$keyword,'cate'=>$cate]);
   }
}
